package diploma.kisel.modelas.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;

import diploma.kisel.modelas.domain.Application;
import diploma.kisel.modelas.validation.Validation;

/**
 * Makes window that allows you to create an application. 
 * 
 * @author Nikolay Kisel
 *
 */
public class ApplicationWindow extends JFrame {

	private int applicationNumberLength = 10;
	private int applicationNameLength = 45;
	private int applicationShortNameLength = 8;

	private JPanel contentPane;
	private JTextField textFieldNumber;
	private JTextField textFieldName;
	private JTextField textFieldShortName;
	private JTextField textFieldCost;

	private static ApplicationWindow frame;	
	
	private WorkplaceWindow currWorkplaceWindow;

	
	/**
	 * Create the frame.
	 */
	public ApplicationWindow() {
		setTitle("Добавить приложение");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 341, 248);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);

		JLabel label = new JLabel("Номер:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 10,
				SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 10,
				SpringLayout.WEST, contentPane);
		contentPane.add(label);

		textFieldNumber = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldNumber, -3, SpringLayout.NORTH, label);
		contentPane.add(textFieldNumber);
		textFieldNumber.setColumns(10);

		JLabel label_1 = new JLabel("Название:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_1, 24,
				SpringLayout.SOUTH, label);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_1, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_1);

		textFieldName = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldName, -3, SpringLayout.NORTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldName, 0, SpringLayout.EAST, textFieldNumber);
		contentPane.add(textFieldName);
		textFieldName.setColumns(10);

		JLabel label_2 = new JLabel("Сокращенное название:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_2, 25,
				SpringLayout.SOUTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_2, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_2);

		textFieldShortName = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldShortName,
				-3, SpringLayout.NORTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldShortName, 0, SpringLayout.EAST, textFieldNumber);
		contentPane.add(textFieldShortName);
		textFieldShortName.setColumns(10);

		JLabel label_3 = new JLabel("Стоимость:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_3, 25,
				SpringLayout.SOUTH, label_2);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_3, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_3);

		textFieldCost = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldCost, -3,
				SpringLayout.NORTH, label_3);
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldCost, 0, SpringLayout.EAST, textFieldNumber);
		contentPane.add(textFieldCost);
		textFieldCost.setColumns(10);

		JButton buttonCancel = new JButton("Отмена");
		buttonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldNumber, 0, SpringLayout.EAST, buttonCancel);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, buttonCancel, 0,
				SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, buttonCancel, -10,
				SpringLayout.EAST, contentPane);
		contentPane.add(buttonCancel);

		JButton buttonAdd = new JButton("Добавить");
		buttonAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Application application = addApplication();
				if (application!=null)
					currWorkplaceWindow.setNewModelForApplication(application);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, buttonAdd, 0,
				SpringLayout.NORTH, buttonCancel);
		sl_contentPane.putConstraint(SpringLayout.EAST, buttonAdd, -6,
				SpringLayout.WEST, buttonCancel);
		contentPane.add(buttonAdd);
	}

	public Application addApplication() {
		Application application = new Application();
		String number = textFieldNumber.getText();
		if (Validation.isLengthBetween(number, 0, applicationNumberLength))
			application.setNumber(number);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неправильно задан номер. Номер может содержать не более "
							+ applicationNumberLength + " символов");
			return null;
		}
		String name = textFieldName.getText();
		if (Validation.isLengthBetween(name, 3, applicationNameLength))
			application.setName(name);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неправильное название. Название может содержать от 3 до "
							+ applicationNameLength + " символов");
			return null;
		}
		String shortName = textFieldShortName.getText();
		if (Validation
				.isLengthBetween(shortName, 0, applicationShortNameLength))
			application.setShortName(shortName);
		else {
			JOptionPane.showMessageDialog(frame,
					"Неправильное сокращенное название. Сокращенное название может быть до "
							+ applicationShortNameLength + " символов");
			return null;
		}
		String fieldCost = textFieldCost.getText();
		if (Validation.isNumber(fieldCost))
			application.setCost(Integer.valueOf(fieldCost));
		else {
			JOptionPane.showMessageDialog(frame,
					"Указана неверная цена");
			return null;
		}		
		return application;
	}

	public static ApplicationWindow getFrame() {
		return frame;
	}

	public static void setFrame(ApplicationWindow frame) {
		ApplicationWindow.frame = frame;
	}

	public WorkplaceWindow getCurrWorkplaceWindow() {
		return currWorkplaceWindow;
	}

	public void setCurrWorkplaceWindow(WorkplaceWindow currWorkplaceWindow) {
		this.currWorkplaceWindow = currWorkplaceWindow;
	}	
	
}
